
#pragma once

// -----------------------------------------------------------------------------

#include "Win32.String.h"

namespace Kif
{
namespace Win32
{

class Number
{
public:

   static Kif::Win32::String *ToString(
      Kif::Win32::String &outString, double number,int precision);

   static inline Kif::Win32::String *ToString(
      Kif::Win32::String &outString, long number)
   {
      TCHAR buff[20];

      errno_t e = _ltot_s(number,buff,SizeOf(buff), 10);

      if (e != 0)
         outString.Dispose();
      else
         outString.Copy(buff);

      return(&outString) ;
   }  // end of convert a long to a string...

   static inline Kif::Win32::String *ToString(
      Kif::Win32::String &outString, int number)
   {
      return(ToString(outString,(long)number));
   }  // end of convert an int to a string...

   static inline Kif::Win32::String *ToString(
      Kif::Win32::String &outString, short number)
   {
      return(ToString(outString,(long)number));
   }  // end of convert a short to a string...

   static inline Kif::Win32::String *ToHexString(
      Kif::Win32::String &outString, HRESULT hResult)
   {
      TCHAR buff[20];
      _stprintf_s(buff, SizeOf(buff), L"%08.8lx", hResult);
      outString.Concat(buff);
      return &outString;
   }  // end of ToString

   static inline Kif::Win32::String *ToString(
      Kif::Win32::String &outString, ULONG number)
   {
      TCHAR buff[20];

      errno_t e;
      if (sizeof(TCHAR) == sizeof(char))
         e = _ultoa_s(number,(char*)buff,SizeOf(buff), 10);
      else
         e = _ultow_s(number,(wchar_t*)buff,SizeOf(buff), 10);

      if (e != 0)
         outString.Dispose();
      else
         outString.Copy(buff);

      return(&outString) ;
   }  // end of convert a short to a string...

   static inline long ToLong(TCHAR *string)
   {
      long r;
      if (*string == NULL)
         return(LONG_MAX);

      try
      {
         r = _ttol(string);
      }
      catch(TCHAR *ex)
      {
         r = LONG_MAX;
         ex = NULL;  // ignore error message
      }
      return(r);
   }  // end of to long

   static inline long ToLong(Kif::Win32::String &string)
   {
      return(ToLong(string.GetBuffer()));
   }  // end of to long

   static inline long ToLong(Kif::Win32::String *string)
   {
      if (string == NULL)
         return(LONG_MAX);
      return(ToLong(string->GetBuffer()));
   }  // end of to long

   static inline double ToDouble(TCHAR *string)
   {
      double r;
      if (*string == NULL)
         return(0.0);

      r = _tstof(string);
      return(r);
   }  // end of to double

   static inline double ToDouble(char *string)
   {
      double r;
      if (*string == NULL)
         return(0.0);

      r = atof(string);
      return(r);
   }  // end of to double

}  ;  // end of Number

}  // end of Kif::Win32
}  // end of Kif

